﻿using System;
using EntityFrameworkExtension.Interface;

namespace MainProcessor.Caching
{
    public class EntityMapper<TU, T>  where T : struct
    {
        private readonly Func<TU, IEfContext, T> find;
        private readonly Func<TU, IEfContext, T> refresh;
        private readonly Func<TU, IEfContext, T> create;

        public EntityMapper(Func<TU, IEfContext, T> find, Func<TU, IEfContext, T> refresh, Func<TU, IEfContext, T> create)
        {
            this.find = find;
            this.refresh = refresh;
            this.create = create;
        }

        public T GetTMapping(TU input, IEfContext efContext)
        {
            var output = find(input, efContext);

            if (output.Equals(default(T)))
            {
                output = refresh(input, efContext);
            }
            if (output.Equals(default(T)))
            {
                output = create(input, efContext);
            }
            return output;
        }
    }
}
